package gui;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import javax.swing.WindowConstants;
import javax.swing.JButton;

import negocios.Fachada;
import negocios.Maquinas;
import negocios.Operacoes;

public class CadastroDeOperacoes extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel lTitulo = null;
	private JLabel lMaq = null;
	private JComboBox cMaq = null;
	private JLabel lCod = null;
	private JFormattedTextField tCod = null;
	private JLabel lDesc = null;
	private JTextField tDesc = null;
	private JLabel lTempo = null;
	private JFormattedTextField tTempo = null;
	private MaskFormatter mTempo = null;
	private JButton Cancelar = null;
	private JButton Confirmar = null;
	private JButton Limpar = null;
	private Maquinas[] x;
	private MaskFormatter mCod = null;

	/**
	 * @param owner
	 */
	public CadastroDeOperacoes(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(425, 254);
		this.setContentPane(getJContentPane());
		this.setContentPane(getJContentPane());
		this.setContentPane(getJContentPane());
		this.setModal(true);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lTempo = new JLabel();
			lTempo.setBounds(new Rectangle(16, 145, 95, 16));
			lTempo.setPreferredSize(new Dimension(86, 20));
			lTempo.setText("Tempo Padrao:");
			lDesc = new JLabel();
			lDesc.setBounds(new Rectangle(15, 113, 71, 16));
			lDesc.setText("Descricao:");
			lCod = new JLabel();
			lCod.setBounds(new Rectangle(13, 83, 123, 16));
			lCod.setText("Codigo da Operacao:");
			lMaq = new JLabel();
			lMaq.setBounds(new Rectangle(14, 59, 63, 16));
			lMaq.setText("Maquina:");
			lTitulo = new JLabel();
			lTitulo.setFont(new Font("Tahoma", Font.BOLD, 25));
			lTitulo.setBounds(new Rectangle(38, 11, 308, 31));
			lTitulo.setText("Cadastro de Operacoes");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lTitulo, BorderLayout.NORTH);
			jContentPane.add(lMaq, null);
			jContentPane.add(getCMaq(), null);
			jContentPane.add(lCod, null);
			jContentPane.add(getTCod(), null);
			jContentPane.add(lDesc, null);
			jContentPane.add(getTDesc(), null);
			jContentPane.add(lTempo, null);
			jContentPane.add(getTTempo(), null);
			jContentPane.add(getCancelar(), null);
			jContentPane.add(getConfirmar(), null);
			jContentPane.add(getLimpar(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes cMaq	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCMaq() {
		if (cMaq == null) {
			cMaq = new JComboBox();
			x = Fachada.getInstancia().listarMaquinas(null, "listar");
			cMaq.setBounds(new Rectangle(162, 54, 173, 20));
			cMaq.addItem("Escolha uma Maquina");
			for(int j=0;j<x.length;j++){
				if(!(x[j].equals(null))){
				cMaq.addItem(x[j].getDescricao());
				}
			 }
			}
		return cMaq;
	}
	
	private MaskFormatter getMCod(){
		if(mCod==null){
			try {
				mCod = new MaskFormatter("####");
				mCod.setPlaceholderCharacter('0');
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		return mCod;
	}


	/**
	 * This method initializes tCod	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JFormattedTextField getTCod() {
		if (tCod == null) {			
			tCod = new JFormattedTextField(getMCod());
			tCod.setBounds(new Rectangle(162, 77, 67, 26));
			tCod.setHorizontalAlignment(JTextField.RIGHT);
			tCod.setEditable(true);
		}
		return tCod;
	}

	/**
	 * This method initializes tDesc	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTDesc() {
		if (tDesc == null) {
			tDesc = new JTextField();
			tDesc.setBounds(new Rectangle(162, 106, 173, 30));
			tDesc.addKeyListener(new java.awt.event.KeyAdapter() {  
				public void keyTyped(java.awt.event.KeyEvent e) {  
                   // int k = e.getKeyChar();  
                    if (tDesc.getText().length() < 100) {  
                         //deixe passar  
                    } else {  
                    	e.setKeyChar((char) KeyEvent.VK_CLEAR);  
                    }  
				}  
    
			});
		}
		return tDesc;
	}

	/**
	 * This method initializes mTempo	
	 * 	
	 * @return javax.swing.text.MaskFormatter	
	 */
	private MaskFormatter getMTempo() {
		if (mTempo == null) {
			try {
				mTempo = new MaskFormatter("#,###");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mTempo.setPlaceholderCharacter('0');
		}
		return mTempo;
	}

	/**
	 * This method initializes tTempo	
	 * 	
	 * @return javax.swing.JFormattedTextField	
	 */
	private JFormattedTextField getTTempo() {
		if (tTempo == null) {
			tTempo = new JFormattedTextField(getMTempo());
			tTempo.setBounds(new Rectangle(162, 141, 86, 26));
			
		}
		return tTempo;
	}

	/**
	 * This method initializes Cancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancelar() {
		if (Cancelar == null) {
			Cancelar = new JButton();
			Cancelar.setBounds(new Rectangle(265, 180, 96, 20));
			Cancelar.setText("Cancelar");
			Cancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					setVisible(false);
					dispose();
				}
			});

		}
		return Cancelar;
	}

	/**
	 * This method initializes Confirmar	
	 * 	
	 * @return javax.swing.JButton	//
	 */
	private JButton getConfirmar() {
		if (Confirmar == null) {
			Confirmar = new JButton();
			Confirmar.setBounds(new Rectangle(16, 180, 96, 20));
			Confirmar.setText("Confirmar");
			Confirmar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e){
					String cod = tCod.getText();
					String desc = tDesc.getText();
					String tempo = tTempo.getText();
					int codigo = Integer.parseInt(cod);
					int chave = ((cMaq.getSelectedIndex())-1);					
					Operacoes operacoes = new Operacoes();
					operacoes.setCodigo(codigo);
					operacoes.setDescricao(desc);
					operacoes.setTempo(tempo);
					try{
						if (chave>=0){
						operacoes.setMaq(x[chave].getGrupo());
						}
					}
						catch (Exception e2){
						JOptionPane.showMessageDialog(null,"Opera��o n�o inserida. Selecione uma m�quina","Confirma��o de inclusao",JOptionPane.ERROR_MESSAGE,null);
						e2.printStackTrace();

					}
					
					
					try {
						if ((tCod.getText().equals(null)) || (tDesc.getText().equals(""))||(tTempo.getText().equals(""))){
							JOptionPane.showMessageDialog(null,"Por favor, preencha todos os campos!","Sig Confex - Erro!",	JOptionPane.INFORMATION_MESSAGE,null);
							tCod.requestFocus();							
						}else{
							
							Operacoes temp = null;
							//Teste de inser��o duplicada							
							temp = Fachada.getInstancia().ConsultarOperacao(operacoes,true);
							
							if ((temp == null) && (chave>=0)){
							
								Fachada.getInstancia().IncluirOperacao(operacoes);
								JOptionPane.showMessageDialog(null,"Operacao inserida com sucesso.","Confirma��o de inclus�o",JOptionPane.INFORMATION_MESSAGE,null);
						
							}else JOptionPane.showMessageDialog(null,"Operacao j� existente ou m�quina n�o selecionada.","Confirma��o de inclusao",JOptionPane.ERROR_MESSAGE,null);
							   
							
						}
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(null,"Opera��o n�o inserida. Selecione uma m�quina","Confirma��o de inclusao",JOptionPane.ERROR_MESSAGE,null);
				e1.printStackTrace();
			}
					tCod.setText("");
					tDesc.setText("");
					tTempo.setText("");
					tCod.requestFocus();
		}});
		}
		return Confirmar;
	}

	/**
	 * This method initializes Limpar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getLimpar() {
		if (Limpar == null) {
			Limpar = new JButton();
			Limpar.setBounds(new Rectangle(137, 180, 96, 20));
			Limpar.setText("Limpar");
			Limpar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					tCod.setText("");
					tDesc.setText("");
					tDesc.setText("");
					tTempo.setText("");
					tCod.requestFocus();
				}
		});}
		return Limpar;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"

